package com.example.medicalrecord.AggregationModule.sickness.service.impl;

import com.example.medicalrecord.AggregationModule.patient.mapper.PatientMapper;
import com.example.medicalrecord.AggregationModule.patient.pojo.dto.PatientQuery;
import com.example.medicalrecord.AggregationModule.patient.pojo.entity.Patient;
import com.example.medicalrecord.AggregationModule.patient.pojo.vo.PatientVO;
import com.example.medicalrecord.AggregationModule.sickness.mapper.SicknessMapper;
import com.example.medicalrecord.AggregationModule.sickness.pojo.dto.SicknessQuery;
import com.example.medicalrecord.AggregationModule.sickness.pojo.vo.SicknessVO;
import com.example.medicalrecord.AggregationModule.sickness.service.SicknessService;
import com.example.medicalrecord.base.response.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SicknessServiceImpl implements SicknessService {
    @Autowired
    SicknessMapper sicknessMapper;
    @Autowired
    PatientMapper patientMapper;

    @Override
    public PageData selectSickness(SicknessQuery sicknessQuery) {
        log.debug("查询病史功能业务:sicknessQuery={}",sicknessQuery);
        PageData pageData = new PageData();
        List<PatientVO> patientVOList = new ArrayList<>();
        if (sicknessQuery.getPatientName()!=null && !sicknessQuery.getPatientName().equals("")) {
//        根据患者名字查询出所有的相应患者信息
            PatientQuery patientQuery = new PatientQuery();
            patientQuery.setPatientName(sicknessQuery.getPatientName());
            patientVOList = patientMapper.select(patientQuery);
        }
        if (patientVOList!=null && patientVOList.size()>0){
//            创建病史类集合用来保存病史数据
            List<SicknessVO> sicknessVOList = new ArrayList<>();
            for (PatientVO patientVO : patientVOList){
                sicknessQuery.setPatientId(patientVO.getId());
                List<SicknessVO> sicknessVOS = sicknessMapper.selectSickness(sicknessQuery);
                sicknessVOList.addAll(sicknessVOS);
            }
            pageData.setTotal(sicknessVOList.size());
            pageData.setList(sicknessVOList);
        }else {
            List<SicknessVO> sicknessVOList = sicknessMapper.selectSickness(sicknessQuery);
            pageData.setTotal(sicknessMapper.selectSicknessCount(sicknessQuery));
            pageData.setList(sicknessVOList);
        }
        return pageData;

    }
}
